/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package au.edu.swin.swinvehicle.managedbean;

import au.edu.swin.swinvehicle.manager.ManagerDetailedTO;
import au.edu.swin.swinvehicle.manager.ManagerManagerException;
import au.edu.swin.swinvehicle.manager.ManagerManagerRemote;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

/**
 *
 * @author Administrator
 */
public class ManagerJSFManagedBean {

    public static final String LIST_MANAGERS = "listManagers";
    public static final String ADD_MANAGER = "addManager";
    public static final String EDIT_MANAGER = "editManager";
    @EJB
    private ManagerManagerRemote managerManagerBean;
    private ManagerDetailedTO manager;

    /** Creates a new instance of ManagerJSFManagedBean */
    public ManagerJSFManagedBean() {
        this.manager = new ManagerDetailedTO();
    }

    public ManagerDetailedTO getManager() {
        return manager;
    }

    public void setManager(ManagerDetailedTO manager) {
        this.manager = manager;
    }

    public List<ManagerDetailedTO> getManagers() {
        List<ManagerDetailedTO> managers = new ArrayList<ManagerDetailedTO>();
        try {
            managers = this.managerManagerBean.getAllDetailedManagers();
        } catch (ManagerManagerException ex) {
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
        }

        return managers;
    }

    public String addManager() {
        try {
            // Check if username is available
            if (this.managerManagerBean.isUsernameAvailable(this.manager.getUsername())) {
                this.managerManagerBean.addManager(this.manager);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Added:");
                msg.setDetail("The new manager has been successfully added");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerSuccessNotification", msg);
                return LIST_MANAGERS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);
                return ADD_MANAGER;
            }
        } catch (ManagerManagerException ex) {
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot add new manager. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);
            return ADD_MANAGER;
        }
    }

    public String editManager() {
        String managerIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("managerId");
        try {
            Long managerId = Long.parseLong(managerIdString);
            this.manager = this.managerManagerBean.getManager(managerId);
            if (this.manager == null) {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid manager ID:");
                msg.setDetail("The manager does not exist. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);
                return LIST_MANAGERS;
            } else {
                return EDIT_MANAGER;
            }
        } catch (ManagerManagerException ex) {
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit manager. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);

            return LIST_MANAGERS;
        }
    }

    public String saveManager() {
        try {
            // Check if the new username is available
            if (this.managerManagerBean.getManager(this.manager.getId()).getUsername().equals(this.manager.getUsername()) || this.managerManagerBean.isUsernameAvailable(this.manager.getUsername())) {
                this.managerManagerBean.editManager(this.manager);
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_INFO);
                msg.setSummary("Updated:");
                msg.setDetail("The manager has been successfully updated");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerSuccessNotification", msg);

                return LIST_MANAGERS;
            } else {
                FacesMessage msg = new FacesMessage();
                msg.setSeverity(FacesMessage.SEVERITY_ERROR);
                msg.setSummary("Invalid username:");
                msg.setDetail("The username is not available. Please try again.");
                FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);

                return EDIT_MANAGER;
            }
        } catch (ManagerManagerException ex) {
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot edit manager. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);
            return EDIT_MANAGER;
        }
    }

    public String deleteManager() {
        String managerIdString = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("managerId");
        try {
            Long managerId = Long.parseLong(managerIdString);
            this.managerManagerBean.deleteManager(managerId);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_INFO);
            msg.setSummary("Deleted:");
            msg.setDetail("The manager has been successfully deleted");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerSuccessNotification", msg);

            return LIST_MANAGERS;
        } catch (ManagerManagerException ex) {
            Logger.getLogger(ManagerJSFManagedBean.class.getName()).log(Level.SEVERE, null, ex);
            FacesMessage msg = new FacesMessage();
            msg.setSeverity(FacesMessage.SEVERITY_ERROR);
            msg.setSummary("Error:");
            msg.setDetail("An error has been occurred, cannot delete manager. Please try again.");
            FacesContext.getCurrentInstance().addMessage("frmNotification:hdnGeneralManagerErrorNotification", msg);

            return LIST_MANAGERS;
        }
    }
}
